<template>
	<view>
		<view style="margin: 10px;">
			<uni-forms  ref="form" :modelValue="model" :rules="rules">
				<uni-forms-item  name="title" label="标题:">
					<uni-easyinput v-model="model.title" placeholder="请输入标题"></uni-easyinput>
				</uni-forms-item>
						<uni-forms-item label="备注">
							<uni-easyinput type="textarea" name="contents" v-model="model.contents" placeholder="请输入备注" />
						</uni-forms-item>
						<button class="button" @click="submit">保存</button>
					</uni-forms>
		</view>
		<view class="line" style="clear:both"></view>
		
	</view>
</template>

<script>
	import comboxSearch from "@/components/cuihai-combox/cuihai-combox.vue"
	import uniSection from "@/components/uni-section.vue"
	import storage from '@/common/storage.js'
	import api from '@/common/api'
	export default {
		components: {
			comboxSearch,
			uniSection
		},
		data() {
			return {
			model:{ id:0,title:'用户留言' },
			isEdit:false,
			project:{},
			rules: {
				title: {
					rules: [{
						required: true,
						errorMessage: '姓名不能为空'
									}],
									
					}
				}
			}
				
		},
		onLoad(option){
			 this.project =storage.getJson('project')
			if(option.id==0){
				this.model.hid = option.hid
				this.model.pid = option.pid
				this.model.address = this.project.address
				this.isEdit = false
			} else {
				this.isEdit = true
				this.getData(option.id)
			}
			
			
		},
		methods: {
			change(e){
				this.model.relation = e
			},
			getValue (e) {
				this.model.relation = e
			},
			getData(id){
				console.log('id',id)
				this.$request.postU(this.$api.CustomerContact.get,{id:id},'form').then(res => {
					if(res.data){
						this.model= res.data
					}
					
				});
			},
			submit() {
				// 在 onLoad 生命周期中，formData添加了一个 id 字段 ，此时这个字段是不参数校验的，所以结果中不返回
				// 在 validate(['id']) 方法中，指定第一个参数 ，即可返回id字段
				this.$refs.form.validate(['id'],(err)=>{
					if(!err){
						if(!this.isEdit){
							this.$request.postU(this.$api.CustomerContact.add,this.model).then(res=>{
								if(res.code==0) {
									uni.showToast({
									title: '新增成功'
									}) 
									this.$storage.setData('refresh','1')
									setTimeout(() =>{
													uni.navigateBack({delta:1  //返回两层页面
													});
												}, 1000);
											
								}
							});
						} else {
							this.$request.postU(this.$api.CustomerContact.update,this.model).then(res=>{
								if(res.code==0) {
									uni.showToast({
									title: '编辑成功'
									}) 
									this.$storage.setData('refresh','1')
									setTimeout(() =>{
													uni.navigateBack({delta: 1  //返回两层页面
													});		
												}, 1000);
									
								}
							});
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>